﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Button1.UniqueID + "').click();return false;}} else {return true}; ");
            TextBox2.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Button1.UniqueID + "').click();return false;}} else {return true}; ");
            Button2.Visible = false;
            if (Page.IsPostBack)
            {
                table_creator();
            }
        }
        private OleDbConnection accessconn;
        DataSet ds;
        //Dynamically creates table for entering the schedule for a week
        public void table_creator()
        {
            accessconn = new OleDbConnection();
            accessconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                @"C:\Dbase_SEA\myDB.accdb";
            PlaceHolder1.Controls.Clear();
            if (TextBox1.Text.Length == 10 && TextBox2.Text.Length == 10)
            {
                accessconn.Open();
                ds = new DataSet();
                DataSet ds1 = new DataSet();
                string query7 = "select Eid, Ename from Employee";
                string query8 = "select TypeName from Type";
                OleDbDataAdapter da = new OleDbDataAdapter(query7, accessconn);
                OleDbDataAdapter da1 = new OleDbDataAdapter(query8, accessconn);
                da.Fill(ds);
                da1.Fill(ds1);
                DateTime date1 = Convert.ToDateTime(TextBox1.Text);
                DateTime date2 = Convert.ToDateTime(TextBox2.Text);
                if (date2.Date == date1.Date.AddDays(6))
                {
                    Table tbl = new Table();
                    PlaceHolder1.Controls.Add(tbl);
                    TableRow tr1 = new TableRow();
                    TableCell col1 = new TableCell();
                    col1.Width = 150;
                    tr1.Cells.Add(col1);
                    TableCell col2 = new TableCell();
                    col2.Width = 150;
                    Label d1 = new Label();
                    d1.ID = "d1";
                    d1.Text = date1.ToShortDateString();
                    //d1.EnableViewState = false;
                    d1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col2.Controls.Add(d1);
                    tr1.Cells.Add(col2);
                    TableCell col3 = new TableCell();
                    col3.Width = 150;
                    Label d2 = new Label();
                    d2.ID = "d2";
                    d2.Text = date1.AddDays(1).ToShortDateString();
                    //d2.EnableViewState = false;
                    d2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col3.Controls.Add(d2);
                    tr1.Cells.Add(col3);
                    TableCell col4 = new TableCell();
                    col4.Width = 150;
                    Label d3 = new Label();
                    d3.ID = "d3";
                    d3.Text = date1.AddDays(2).ToShortDateString();
                    //d3.EnableViewState = false;
                    d3.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col4.Controls.Add(d3);
                    tr1.Cells.Add(col4);
                    TableCell col5 = new TableCell();
                    col5.Width = 150;
                    Label d4 = new Label();
                    d4.ID = "d4";
                    d4.Text = date1.AddDays(3).ToShortDateString();
                    d4.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col5.Controls.Add(d4);
                    tr1.Cells.Add(col5);
                    TableCell col6 = new TableCell();
                    col6.Width = 150;
                    Label d5 = new Label();
                    d5.ID = "d5";
                    d5.Text = date1.AddDays(4).ToShortDateString();
                    d5.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col6.Controls.Add(d5);
                    tr1.Cells.Add(col6);
                    TableCell col7 = new TableCell();
                    col7.Width = 150;
                    Label d6 = new Label();
                    d6.ID = "d6";
                    d6.Text = date1.AddDays(5).ToShortDateString();
                    d6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col7.Controls.Add(d6);
                    tr1.Cells.Add(col7);
                    TableCell col8 = new TableCell();
                    col8.Width = 150;
                    Label d7 = new Label();
                    d7.ID = "d7";
                    d7.Text = date1.AddDays(6).ToShortDateString();
                    d7.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                    col8.Controls.Add(d7);
                    tr1.Cells.Add(col8);

                    tbl.Rows.Add(tr1);

                    // Now iterate through the table and add your controls  
                    //the row count is determined by the number of employees in the organization
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                     {
                         TableRow tr = new TableRow();
                         TableCell tc1 = new TableCell();
                         tc1.Width = 150;
                         Label emp = new Label();
                         emp.Text = ds.Tables[0].Rows[i]["Eid"].ToString() + "<br>" + ds.Tables[0].Rows[i]["Ename"].ToString();
                         emp.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc1.Controls.Add(emp);
                         tr.Cells.Add(tc1);
                         TableCell tc2 = new TableCell();
                         tc2.Width = 150;
                         TextBox t1 = new TextBox();
                         t1.ID = "shift1emp" + Convert.ToString(i + 1);
                         DropDownList ddl1 = new DropDownList();
                         ddl1.ID = "type1emp" + Convert.ToString(i + 1);
                         ddl1.AutoPostBack = false;
                         ddl1.Width = 150;
                         ddl1.DataSource = ds1.Tables[0];
                         ddl1.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl1.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl1.DataBind();
                         ListItem item1 = new ListItem();
                         item1.Text = "--Select--";
                         ddl1.Items.Insert(0, item1);
                         //d1.EnableViewState = false;
                         //d1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc2.Controls.Add(t1);
                         tc2.Controls.Add(ddl1);
                         tr.Cells.Add(tc2);
                         TableCell tc3 = new TableCell();
                         tc3.Width = 150;
                         TextBox t2 = new TextBox();
                         t2.ID = "shift2emp" + Convert.ToString(i + 1);
                         DropDownList ddl2 = new DropDownList();
                         ddl2.ID = "type2emp" + Convert.ToString(i + 1);
                         ddl2.AutoPostBack = false;
                         ddl2.Width = 150;
                         ddl2.DataSource = ds1.Tables[0];
                         ddl2.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl2.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl2.DataBind();
                         ListItem item2 = new ListItem();
                         item2.Text = "--Select--";
                         ddl2.Items.Insert(0, item2);
                         //d2.EnableViewState = false;
                         //d2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc3.Controls.Add(t2);
                         tc3.Controls.Add(ddl2);
                         tr.Cells.Add(tc3);
                         TableCell tc4 = new TableCell();
                         tc4.Width = 150;
                         TextBox t3 = new TextBox();
                         t3.ID = "shift3emp" + Convert.ToString(i + 1);
                         DropDownList ddl3 = new DropDownList();
                         ddl3.ID = "type3emp" + Convert.ToString(i + 1);
                         ddl3.AutoPostBack = false;
                         ddl3.Width = 150;
                         ddl3.DataSource = ds1.Tables[0];
                         ddl3.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl3.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl3.DataBind();
                         ListItem item3 = new ListItem();
                         item3.Text = "--Select--";
                         ddl3.Items.Insert(0, item3);
                         //d3.EnableViewState = false;
                         //d3.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc4.Controls.Add(t3);
                         tc4.Controls.Add(ddl3);
                         tr.Cells.Add(tc4);
                         TableCell tc5 = new TableCell();
                         tc5.Width = 150;
                         TextBox t4 = new TextBox();
                         t4.ID = "shift4emp" + Convert.ToString(i + 1);
                         DropDownList ddl4 = new DropDownList();
                         ddl4.ID = "type4emp" + Convert.ToString(i + 1);
                         ddl4.AutoPostBack = false;
                         ddl4.Width = 150;
                         ddl4.DataSource = ds1.Tables[0];
                         ddl4.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl4.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl4.DataBind();
                         ListItem item4 = new ListItem();
                         item4.Text = "--Select--";
                         ddl4.Items.Insert(0, item4);
                         //d4.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc5.Controls.Add(t4);
                         tc5.Controls.Add(ddl4);
                         tr.Cells.Add(tc5);
                         TableCell tc6 = new TableCell();
                         tc6.Width = 150;
                         TextBox t5 = new TextBox();
                         t5.ID = "shift5emp" + Convert.ToString(i + 1);
                         DropDownList ddl5 = new DropDownList();
                         ddl5.ID = "type5emp" + Convert.ToString(i + 1);
                         ddl5.AutoPostBack = false;
                         ddl5.Width = 150;
                         ddl5.DataSource = ds1.Tables[0];
                         ddl5.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl5.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl5.DataBind();
                         ListItem item5 = new ListItem();
                         item5.Text = "--Select--";
                         ddl5.Items.Insert(0, item5);
                         //d5.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc6.Controls.Add(t5);
                         tc6.Controls.Add(ddl5);
                         tr.Cells.Add(tc6);
                         TableCell tc7 = new TableCell();
                         tc7.Width = 150;
                         TextBox t6 = new TextBox();
                         t6.ID = "shift6emp" + Convert.ToString(i + 1);
                         DropDownList ddl6 = new DropDownList();
                         ddl6.ID = "type6emp" + Convert.ToString(i + 1);
                         ddl6.AutoPostBack = false;
                         ddl6.Width = 150;
                         ddl6.DataSource = ds1.Tables[0];
                         ddl6.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl6.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl6.DataBind();
                         ListItem item6 = new ListItem();
                         item6.Text = "--Select--";
                         ddl6.Items.Insert(0, item6);
                         //d6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc7.Controls.Add(t6);
                         tc7.Controls.Add(ddl6);
                         tr.Cells.Add(tc7);
                         TableCell tc8 = new TableCell();
                         tc8.Width = 150;
                         TextBox t7 = new TextBox();
                         t7.ID = "shift7emp" + Convert.ToString(i + 1);
                         DropDownList ddl7 = new DropDownList();
                         ddl7.ID = "type7emp" + Convert.ToString(i + 1);
                         ddl7.AutoPostBack = false;
                         ddl7.Width = 150;
                         ddl7.DataSource = ds1.Tables[0];
                         ddl7.DataTextField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl7.DataValueField = ds1.Tables[0].Columns["TypeName"].ColumnName.ToString();
                         ddl7.DataBind();
                         ListItem item7 = new ListItem();
                         item7.Text = "--Select--";
                         ddl7.Items.Insert(0, item7);
                         //d7.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                         tc8.Controls.Add(t7);
                         tc8.Controls.Add(ddl7);
                         tr.Cells.Add(tc8);

                         tbl.Rows.Add(tr);
                     } Button2.Visible = true;
                }
                else MessageBox("Please Prepare Schedule for a Week");
                accessconn.Close();
            }
            else MessageBox("Invalid Format");
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Button2.Visible == true)
            {
                table_creator();
            }
        }

        //save the schedule data into the database
        protected void Button2_Click(object sender, EventArgs e)
        {
            accessconn.Open();
            for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 1; j <= 7; j++)
                {
                    if (((TextBox)Page.FindControl("shift" + j + "emp" + i)).Text != "")
                    {
                        string empid = ds.Tables[0].Rows[i]["Eid"].ToString();
                        string shiftdate = ((Label)Page.FindControl("d" + j)).Text;
                        string shifttime = ((TextBox)Page.FindControl("shift" + j + "emp" + i)).Text;
                        string jobtype = ((DropDownList)Page.FindControl("type" + j + "emp" + i)).SelectedItem.Text;
                        string query9 = "INSERT INTO Schedule([Eid],[ShiftDate],[ShiftTime],[JobType]) VALUES ('" + empid + "','" + shiftdate + "','" + shifttime + "','" + jobtype + "')";
                        OleDbCommand comm = new OleDbCommand(query9, accessconn);
                        comm.ExecuteNonQuery();
                    }
                }
            }
            accessconn.Close();
        }

        protected void home_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:62505/Default.aspx");
        }
    }
}
